export default function updater(state, action){
    let list;
    let list2;
    switch(action.type){
        case 'select':
            list = state.list.slice();
            list2 = state.list2.slice();
            list2.forEach(item=>{
                item.selected = false;
            });
            let list2Id = list2.map(item=>item.id);
            for(let i=0; i<list.length; i++){
                let item = list[i];
                if(item.id===action.id){
                    item.selected = true;
                    if(!list2Id.includes(item.id)){
                        list2.push(item);
                        break;
                    }
                }
            }
            return Object.assign({}, state, {list,list2});
            break;
        case 'close':
            list = state.list.slice();
            list2 = state.list2.slice();
            for(let i=0; i<list2.length; i++){
                let item = list2[i];
                if(item.id===action.id){
                    //item.selected = true;
                    list2.splice(i,1);
                }
            }
            // list.sort(function(item1,item2){
            //     return item1.selected>item2.selected ? 1 : item1.selected<item2.selected ? -1 : 0;
            // });
            return Object.assign({}, state, {list,list2});
            break;
        default:
            return state;
    }

}
